package com.transfiguro.datamodel.csmodels;

import com.transfiguro.datamodel.ClassType;

import java.util.ArrayList;
import java.util.List;

public class CSClassModel {
    private String className;
    private String nameSpace;
    private ClassType classType;
    private List<CSVariable> variables;
    private List<CSMethod> methods;
    private static final String NAMESPACE_PREFIX = "namespace DefaultNamepace";
    private Usings usings;
    private String accessModifier;
    private double conversionTime;
    private List<String> parents;

    public String getAccessModifier() {
        return accessModifier;
    }

    public void setAccessModifier(final String accessModifier) {
        this.accessModifier = accessModifier;
    }

    public CSClassModel() {
        parents = new ArrayList<String>();
        usings = new Usings();
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(final String className) {
        this.className = className;
    }

    public ClassType getClassType() {
        return classType;
    }

    public void setClassType(final ClassType classType) {
        this.classType = classType;
    }

    public List<CSVariable> getVariables() {
        return variables;
    }

    public void setVariables(final List<CSVariable> variables) {
        this.variables = variables;
    }

    public List<CSMethod> getMethods() {
        return methods;
    }

    public void setMethods(final List<CSMethod> methods) {
        this.methods = methods;
    }

    public String getNameSpace() {
        return nameSpace;
    }

    public void setNameSpace(final String nameSpace) {
        this.nameSpace = nameSpace;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(usings.toString());
        if (classType == ClassType.BASECLASS) {
            stringBuilder.append(NAMESPACE_PREFIX).append(nameSpace != null ? nameSpace : "").append(" {\n")
                    .append(accessModifier).append(' ').append("class").append(' ')
                    .append(className);


            if (hasParents()) {
                stringBuilder.append(": ");
                stringBuilder.append(getParentString());
            }

            stringBuilder.append(" {\n");
            for (final CSVariable variable : variables) {
                stringBuilder.append(variable.toString()).append(";\n");
            }
            stringBuilder.append("\n");
            for (final CSMethod method : methods) {
                stringBuilder.append(method.toString()).append("\n");
            }

        } else {
            stringBuilder.append(NAMESPACE_PREFIX).append(nameSpace != null ? nameSpace : "").append(" {\n")
                    .append(accessModifier).append(' ').append("interface").append(' ')
                    .append(className).append(" {\n");
            for (final CSMethod method : methods) {
                stringBuilder.append(method.getInterfaceStyledMethod());
            }
        }
        stringBuilder.append("\n}");
        stringBuilder.append("\n}");
        return stringBuilder.toString();

    }

    public Usings getUsings() {
        return usings;
    }

    public void setUsings(final Usings usings) {
        this.usings = usings;
    }

    public double getConversionTime() {
        return conversionTime;
    }

    public void setConversionTime(final double conversionTime) {
        this.conversionTime = conversionTime;
    }

    public List<String> getParents() {
        return parents;
    }

    public void setParents(final List<String> parents) {
        this.parents = parents;
    }

    public boolean hasParents() {
        return parents.isEmpty();
    }

    public String getParentString() {
        StringBuilder result = new StringBuilder();
        for(int index = 0; index<parents.size(); index++) {
            result.append(parents.get(index));
            if(index < parents.size() - 1) {
                result.append(',');
            }
        }
        return result.toString();
    }
}
